package top.dingwen.io.treasure.encrypt.api.autoconfigure;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import top.dingwen.io.treasure.encrypt.api.constant.EncryptApiConstant;
import top.dingwen.io.treasure.encrypt.api.filter.CryptoFilter;
import top.dingwen.io.treasure.encrypt.api.web.EncryptApiIndexController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.DispatcherType;
import java.util.List;

/**
 * EncryptAutoconfiguration : 自动配置类
 *
 * @author dingwen
 * @since 2024/10/16 13:28
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(EncryptApiProperties.class)
@Lazy(value = false)
@Slf4j
public class EncryptApiAutoconfiguration {

    @Resource
    private EncryptApiProperties encryptApiProperties;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        if (log.isInfoEnabled()) {
            log.info("{}\n\n" +
                    "                                   _                     _ \n" +
                    "                                  | |                   (_)\n" +
                    "   ___ _ __   ___ _ __ _   _ _ __ | |_ ______ __ _ _ __  _ \n" +
                    "  / _ / '_  / __| '__| | | | '_ /| __|______/ _` | '_ // |\n" +
                    " |  __/ | | | (__| |  | |_| | |_) | |_      | (_| | |_) | |\n" +
                    "  /___|_| |_|/___|_|   //__, | .__/ /__|      /__,_| .__/|_|\n" +
                    "                        __/ | |                   | |      \n" +
                    "                      |___/|_|                   |_|      ", EncryptApiConstant.EN_L_P_S);
            log.info("{},读取到的配置如下:\n{}", EncryptApiConstant.EN_L_P_S, JSONUtil.toJsonPrettyStr(encryptApiProperties));
            log.info("{}，启动成功", EncryptApiConstant.EN_L_P_S);
        }
    }

    /**
     * 加密过滤器
     *
     * @return {@link FilterRegistrationBean }<{@link CryptoFilter }>
     */
    @ConditionalOnProperty(name = "dingwen.treasure.encrypt.api.enabled", havingValue = "true")
    @Bean
    public FilterRegistrationBean<CryptoFilter> cryptoFilterRegistration() {
        List<String> interceptUrls = encryptApiProperties.getInterceptUrls();
        if (CollUtil.isEmpty(interceptUrls)) {
            interceptUrls.add(EncryptApiConstant.DEFAULT_INTERCEPT_URL);
        }
        FilterRegistrationBean<CryptoFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new CryptoFilter(encryptApiProperties));
        registration.addUrlPatterns(interceptUrls.toArray(new String[0]));
        registration.setName(EncryptApiConstant.EN_FILTER_NAME);
        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        log.info("{},{},已经成功加载", EncryptApiConstant.EN_L_P_S, EncryptApiConstant.EN_L_P_CF);
        return registration;
    }

    /**
     * 默认API
     *
     * @return {@link EncryptApiIndexController }
     */
    @ConditionalOnProperty(name = "dingwen.treasure.encrypt.api.defaultApiEnabled", havingValue = "true")
    @Bean
    public EncryptApiIndexController encryptIndexController() {
        log.info("{},{},已经成功加载", EncryptApiConstant.EN_L_P_S, EncryptApiConstant.EN_L_P_API);
        return new EncryptApiIndexController();
    }
}
